/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NicholasDatabase;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author ChuckToh
 */
public class DatabaseInterface {

    private static DatabaseInterface dbi;
    private static String dbDriver, connectionString;
    protected Connection theConnection;

    private DatabaseInterface() throws Exception {
        //Loading Sun's JDBC ODBC Driver
        setConnectionDriverAndString();
        Class.forName(dbDriver);
    }

    public static DatabaseInterface getDBI() throws Exception {
        if (dbi == null) {
            dbi = new DatabaseInterface();
        }
        return dbi;
    }

    public Connection getConnection() throws Exception {
        if (theConnection == null || theConnection.isClosed()) {
            theConnection = DriverManager.getConnection(connectionString);
        }
        return theConnection;
    }

    public void closeConnection() throws Exception {
        theConnection.close();
    }

    private void setConnectionDriverAndString() {
        dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
        connectionString = "jdbc:odbc:thinkquest";
    }
}
